package com.huobao.yunying.fragments;

import java.util.List;
import java.util.Map;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.jjobes.slidedatetimepicker.CustomDatePicker;
import com.github.jjobes.slidedatetimepicker.SlideDateTimePicker;
import com.huobao.yunying.activitys.Act_Baitiao;
import com.huobao.yunying.activitys.Act_DingDanGuanli;
import com.huobao.yunying.activitys.Act_Dongtai_List;
import com.huobao.yunying.activitys.Act_Gonghuoshang;
import com.huobao.yunying.activitys.Act_Kefu;
import com.huobao.yunying.activitys.Act_Message_TypeList;
import com.huobao.yunying.activitys.Act_MyHongbao;
import com.huobao.yunying.activitys.Act_MySave;
import com.huobao.yunying.activitys.Act_MyYouhuiquan;
import com.huobao.yunying.activitys.Act_PianHao_SheZhi;
import com.huobao.yunying.activitys.Act_SeeHistory;
import com.huobao.yunying.activitys.Act_YijianFankui;
import com.huobao.yunying.activitys_hehuoren.Act_Hehuoren;
import com.huobao.yunying.entity.UserInfo;
import com.huobao.yunying.entity.User_buyer_stat;
import com.huobao.yunying.entity.Userugrade;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.OpenBrowUtils;
import com.huobao.yunying.utils.SessionUtils;
import com.huobao.yunying.utils.UserInfoUtils;
import com.huobao.yunying.views.RoundImageView;
import com.huobao.zhangying.R;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Fragment_Mine extends Fragment implements OnClickListener {
	private LinearLayout Lin_MyShezhi, Lin_MyDingDan, Lin_DaiFuKuan, Lin_DaiPingJia, Lin_DaiQueRen;
	private Handler handler = new Handler();
	/** 用户头像 */
	private RoundImageView img_userphoto;
	/*** 用户名 **/
	private TextView tv_name;
	/** 用户等级 */
	private TextView tv_level;
	
	//代付款、待确认、带评价
	private TextView tv_pendding,tv_shipped,tv_finished;
	private View llyt_msg;//我的消息
	private View llyt_save;//我的收藏
	private View llyt_zuji;//我的足迹
	
	private View llyt_kefu;//客服中心
	private View llyt_fankui;//意见反馈
	private View llyt_gonghuoshang ;//本地供货商
	
	private View llyt_youhuiquan;//优惠券
	private View llyt_hongbao;//红包
	
	private View llyt_guanyu;//关于我们
	
	private View llyt_baitiao;//掌婴白条
	private TextView tv_baitiao_hint;//审核中提示
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_my, null);
		tv_baitiao_hint = (TextView) view.findViewById(R.id.tv_baitiao_hint);
		llyt_baitiao = view.findViewById(R.id.llyt_baitiao);
		Lin_MyShezhi = (LinearLayout) view.findViewById(R.id.my_shezhi);
		Lin_MyDingDan = (LinearLayout) view.findViewById(R.id.my_dingdan);
		Lin_DaiFuKuan = (LinearLayout) view.findViewById(R.id.daifukuan);
		Lin_DaiQueRen = (LinearLayout) view.findViewById(R.id.daiqueren);
		llyt_gonghuoshang = view.findViewById(R.id.llyt_1);
		llyt_kefu = view.findViewById(R.id.llyt_5);
		llyt_fankui = view.findViewById(R.id.llyt_30);
		llyt_guanyu = view.findViewById(R.id.llyt_10);
		llyt_youhuiquan = view.findViewById(R.id.llyt_11);
		llyt_hongbao = view.findViewById(R.id.llyt_12);
		llyt_youhuiquan.setOnClickListener(this);
		llyt_hongbao.setOnClickListener(this);
		Lin_DaiPingJia = (LinearLayout) view.findViewById(R.id.daipingjia);
		img_userphoto = (RoundImageView) view.findViewById(R.id.img_userphoto);
		llyt_save = view.findViewById(R.id.llyt_2);
		EventBus.getDefault().register(this);
		tv_pendding = (TextView) view.findViewById(R.id.tv_pendding);
		tv_shipped = (TextView) view.findViewById(R.id.tv_shipped);
		tv_finished = (TextView) view.findViewById(R.id.tv_finished);
		llyt_msg = view.findViewById(R.id.llyt_msg);
		llyt_zuji = view.findViewById(R.id.llyt_3);
		img_userphoto.setRectAdius(90);
		Lin_MyShezhi.setOnClickListener(this);
		Lin_MyDingDan.setOnClickListener(this);
		Lin_DaiFuKuan.setOnClickListener(this);
		llyt_guanyu.setOnClickListener(this);
		Lin_DaiQueRen.setOnClickListener(this);
		Lin_DaiPingJia.setOnClickListener(this);
		llyt_baitiao.setOnClickListener(this);
		llyt_zuji.setOnClickListener(this);
		llyt_msg.setOnClickListener(this);
		llyt_save.setOnClickListener(this);
		tv_name = (TextView) view.findViewById(R.id.tv_name);
		tv_level = (TextView) view.findViewById(R.id.tv_level);
		llyt_kefu.setOnClickListener(this);
		llyt_fankui.setOnClickListener(this);
		llyt_gonghuoshang.setOnClickListener(this);
		return view;
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
	@Subscriber(tag = "uploadPhotoSuccess")
	public void uploadPhotoSuccess(String s){
		getUserMsg();
	}

	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (isVisibleToUser) {
			if (SessionUtils.getRole(getActivity()) == 3) {
				Intent intent = new Intent(getActivity(), Act_Hehuoren.class);
				startActivity(intent);
				return;
			}
			// 可见时执行的操作
			getUserMsg();
		} else {
			// 不可见时执行的操作
		}
	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.my_shezhi:
			startActivity(new Intent(getActivity(), Act_PianHao_SheZhi.class));
			break;
		case R.id.my_dingdan:
			startActivity(new Intent(getActivity(), Act_DingDanGuanli.class));
			break;
		case R.id.daifukuan:
			startActivity(new Intent(getActivity(), Act_DingDanGuanli.class).putExtra("pos", 4));
			break;
		case R.id.daiqueren:
			startActivity(new Intent(getActivity(), Act_DingDanGuanli.class).putExtra("pos", 2));
			break;
		case R.id.daipingjia:
			startActivity(new Intent(getActivity(), Act_DingDanGuanli.class).putExtra("pos", 5));
			break;
		case R.id.llyt_msg:
			startActivity(new Intent(getActivity(), Act_Message_TypeList.class));
			break;
		case R.id.llyt_2://收藏
			startActivity(new Intent(getActivity(), Act_MySave.class));
			break;
		case R.id.llyt_3://收藏
			startActivity(new Intent(getActivity(), Act_SeeHistory.class));
			break;
		case R.id.llyt_10://关于
			OpenBrowUtils.startBrower(getActivity(), "http://m.zhangyingtianxia.com/index.php?app=about&about=app");
			break;
		case R.id.llyt_5://客服中心
			startActivity(new Intent(getActivity(), Act_Kefu.class));
			break;
		case R.id.llyt_30://意见反馈
			startActivity(new Intent(getActivity(), Act_YijianFankui.class));
			break;
		case R.id.llyt_1://供货商
			startActivity(new Intent(getActivity(), Act_Gonghuoshang.class));
			break;
		case R.id.llyt_11://优惠券
			startActivity(new Intent(getActivity(), Act_MyYouhuiquan.class));
			break;
		case R.id.llyt_12://红包
			startActivity(new Intent(getActivity(), Act_MyHongbao.class));
			break;
		case R.id.llyt_baitiao://掌婴白条
			if(tv_baitiao_hint.getVisibility() == View.VISIBLE){
				Toast.makeText(getActivity(), "白条正在审核中...", Toast.LENGTH_SHORT).show();
				return;
			}
			startActivity(new Intent(getActivity(), Act_Baitiao.class).putExtra("id", user_id));
			break;
		}
	}

	private String user_id;
	private void getUserMsg() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, "member");
		Http.getInstant().sendRequest(getActivity(), false, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				String my_biaotiao = jsonObject.getString("my_biaotiao");
				if(TextUtils.isEmpty(my_biaotiao) == false && my_biaotiao.equals("null") == false){
					tv_baitiao_hint.setVisibility(View.VISIBLE );
				}else{
					tv_baitiao_hint.setVisibility(View.GONE);
				}
				UserInfo user = JSON.parseObject(jsonObject.getString("user_info"), UserInfo.class);
				UserInfoUtils.saveUserInfo(getActivity(), user);
				user_id = user.user_id;
				JSONObject jsonObject1 = jsonObject.getJSONObject("user_info");
				List<Userugrade> list = JSON.parseArray(jsonObject1.getString("ugrade"), Userugrade.class);
				Userugrade userugrade = null;
				if (list != null && list.size() != 0)
					userugrade = list.get(0);
				user.Userugrade = userugrade;
				User_buyer_stat buyer_stat = JSON.parseObject(jsonObject.getString("buyer_stat"),
						User_buyer_stat.class);
				//购物车数量
				String cart_num = jsonObject.getJSONObject("cart_data").getString("cart_num");
				showUserMsg(user, buyer_stat);
			}

			@Override
			public void onHttpError(String error) {
				EventBus.getDefault().post("", "GetUserMessageFail");
			}
		});
	}

	protected void showUserMsg(UserInfo user, User_buyer_stat buyer_stat) {
		ImageUtils.loadimg(img_userphoto, user.portrait);
		tv_name.setText(user.user_name == null ? "--" : user.user_name);
		if (user.Userugrade == null)
			tv_level.setText("");
		else
			tv_level.setText(user.Userugrade.grade_name == null ? "--" : user.Userugrade.grade_name);
		tv_pendding.setText(buyer_stat.pending);
		tv_shipped.setText(buyer_stat.shipped);
		tv_finished.setText(buyer_stat.finished);
	}

}
